﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.Globalization
{
    public class CultureItemCollection : System.Collections.ObjectModel.KeyedCollection<string, CultureItem>
    {
        #region Public Members

        #region CurrentValue

        public CultureItem CurrentValue
        {
            get
            {
                Sidvall.SystemContext.Current.CultureManager.EnsureCorrectCurrentUICulture();
                return GetItem(System.Globalization.CultureInfo.CurrentUICulture.Name);
            }
        }

        #endregion

        #region Add

        public void Add(string name, string displayName, string englishName)
        {
            var item = new CultureItem()
            {
                Name = name,
                DisplayName = displayName,
                EnglishName = englishName,
            };
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<CultureItem> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
                Add(item);
        }
        public void AddRange(params CultureItem[] items)
        {
            AddRange(items.AsEnumerable());
        }

        #endregion
        #region ContainsCulture

        public bool ContainsCulture(string name)
        {
            var item = GetItem(name);
            return (item != null);
        }

        #endregion
        #region Copy

        public CultureItemCollection Copy()
        {
            var items = new CultureItemCollection();
            foreach (var item in this)
                items.Add(item.Copy());
            return items;
        }

        #endregion
        #region GetItem

        public CultureItem GetItem(string name)
        {
            if (!string.IsNullOrWhiteSpace(name))
            {
                if (Contains(name))
                    return this[name];
                var index = name.LastIndexOf('-');
                while (index > 0)
                {
                    name = name.Substring(0, index);
                    if (Contains(name))
                        return this[name];
                    index = name.LastIndexOf('-');
                }
            }
            return null;
        }

        #endregion
        #region GetKeyForItem

        protected override string GetKeyForItem(CultureItem item)
        {
            if (item == null)
                throw new System.ArgumentNullException(nameof(item));
            return item.Name;
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.CurrentValue.DisplayName;
        }

        #endregion

        #endregion
        #region Factory Members

        #region CreateInstance

        public static CultureItemCollection CreateInstance(IEnumerable<CultureItem> values)
        {
            return new CultureItemCollection(values);
        }
        public static CultureItemCollection CreateInstance(params CultureItem[] values)
        {
            return new CultureItemCollection(values);
        }
        public static CultureItemCollection CreateInstance(string cultureName1, string cultureDisplayName1, string cultureEnglishName1)
        {
            var items = new CultureItemCollection();
            items.Add(cultureName1, cultureDisplayName1, cultureEnglishName1);
            return items;
        }
        public static CultureItemCollection CreateInstance(string cultureName1, string cultureDisplayName1, string cultureEnglishName1,
            string cultureName2, string cultureDisplayName2, string cultureEnglishName2)
        {
            var items = new CultureItemCollection();
            items.Add(cultureName1, cultureDisplayName1, cultureEnglishName1);
            items.Add(cultureName2, cultureDisplayName2, cultureEnglishName2);
            return items;
        }
        public static CultureItemCollection CreateInstance(string cultureName1, string cultureDisplayName1, string cultureEnglishName1,
            string cultureName2, string cultureDisplayName2, string cultureEnglishName2,
            string cultureName3, string cultureDisplayName3, string cultureEnglishName3)
        {
            var items = new CultureItemCollection();
            items.Add(cultureName1, cultureDisplayName1, cultureEnglishName1);
            items.Add(cultureName2, cultureDisplayName2, cultureEnglishName2);
            items.Add(cultureName3, cultureDisplayName3, cultureEnglishName3);
            return items;
        }

        #endregion

        #endregion
        #region Constructors

        public CultureItemCollection() : base(System.StringComparer.OrdinalIgnoreCase)
        {
        }
        public CultureItemCollection(IEnumerable<CultureItem> values) : base(System.StringComparer.OrdinalIgnoreCase)
        {
            AddRange(values);
        }
        public CultureItemCollection(params CultureItem[] values) : base(System.StringComparer.OrdinalIgnoreCase)
        {
            AddRange(values);
        }

        #endregion
    }
}
